package com.carrental.api.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 骆驼和下线转换器实用程序
 *
 * @author 梁毅
 */
public class CamelAndUnderLineConverterUtil {

    private static final Pattern HUMP_PATTERN = Pattern.compile("[A-Z]");

    /**
     * 驼峰转下划线
     *
     * @return String
     */
    public static String getColumnName(String str) {
        if (str == null) {
            return null;
        }
        Matcher matcher = HUMP_PATTERN.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
